//
//  TTDiamondDetailViewController.m
//  TaoTuo
//
//  Created by 周晨晔 on 2019/9/18.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDiamondDetailViewController.h"
#import "TTDiamondDetailInfoViewCell.h"
#import "TTDiamondDetailBigViewCell.h"
#import "TTDetailImageViewCell.h"
#import "TTHomeListTableViewCell.h"
#import "TTDetailSectionLabel.h"
#import "TTDiamondDetailSectionView.h"
#import "TTShopCartView.h"
#import "TTShopCartViewController.h"
#import "TTGoodsDetailViewController.h"
#import "LLWebViewController.h"
#import "YBImageBrowser.h"
#import "TTCommondDataModel.h"
#import "TTCommitOrderViewController.h"
#import "TTDiamondDetailGrayInfoViewCell.h"
#import "TTDiamondDetailImageInfoViewCell.h"
#import "TTDiamondDetailZuanshiImageInfoViewCell.h"
#import "TTDiamondPopShareView.h"

#define cellType @"cellType"
#define cellContent @"cellContent"
#define notDataViewH 260

#define bottomViewH 50

typedef enum : NSUInteger {
    CellTypeWithInfo,
    CellTypeWithMultyInfo,
    CellTypeWithNormalInfo,
    CellTypeWithZuanshiImageInfo,
    CellTypeWithGrayInfo,
    CellTypeWithImageInfo,
    CellTypeWithImage,
    CellTypeWithShopList,
} CellType;

static NSString *infoCellID = @"diamond_info_cell";
static NSString *grayCellID = @"diamond_gray_cell";
static NSString *zuanshiCellID = @"diamond_zuanshi_cell";
static NSString *imageInfoCellID = @"diamond_inage_info_cell";
static NSString *imageCellID = @"diamond_image_cell";
static NSString *listCellID = @"diamond_list_cell";

@interface TTDiamondDetailViewController ()<UITableViewDelegate, UITableViewDataSource, TTDiamondDetailInfoViewCellDelegate, TTDiamondDetailImageInfoViewCellDelegate>
{
    UIButton *eyeBtn_;
    NSString *certNum_; // 证书编号
    NSString *rmbPrice_; // 人民币/粒
    NSString *dollarPrice_; // 美元/粒
    NSString *internationalPrice_; // 国际报价
    NSString *discountStr_; // 折扣
    NSString *yaomaStr_; // 腰码
}

@property (nonatomic, strong) TTDiamondModel *model;
@property (nonatomic, copy) NSString *diamondID;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIView *listSectionView;
@property (nonatomic, strong) UILabel *countLabel;

//@property (nonatomic, assign) BOOL isOpen;  // 展开
@property (nonatomic, assign) CGFloat imageH;

//@property (nonatomic, assign) BOOL finished;    // 网络请求成功
//@property (nonatomic, strong) UIView *notDataView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableDictionary *paramsDic;   // 参数

@property (nonatomic, strong) NSArray *sectionTitles;

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) TTShopCartView *shopCartView;
@property (nonatomic, strong) UIButton *addCartBtn; //加入购物车

@property (nonatomic, assign) int tuoCount; // 拖的数量
@property (nonatomic, assign) BOOL isInCart;    // 是否在购物车中
@property (nonatomic, assign) BOOL statusChange;    // 是否取消或加入了购物车

@property (nonatomic, assign) BOOL hasPic;  // 是否有那四张图片

@end

@implementation TTDiamondDetailViewController

- (instancetype)initWithDiamondModel:(nullable TTDiamondModel *)model diamondID:(nullable NSString *)diamondID
{
    self = [super init];
    if (self) {
        
        self.model = model;
        self.diamondID = diamondID;
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (self.statusChange && self.dismissBlock) {
        self.dismissBlock();
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"裸石详情";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIImage *image = [UIImage imageNamed:@"luoshidetailzs"];
    CGFloat h = self.view.width * image.size.height / image.size.width;
    self.imageH = h;
    
    self.statusChange = NO;
    [self getDiamondDetailData];
    
    
}

///分享
- (void)shareAction
{
    TTDiamondPopShareView *popShareView = [TTDiamondPopShareView createViewFromNib];
    popShareView.model = self.model;
    [TYShowAlertView showAlertViewWithView:popShareView backgoundTapDismissEnable:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    NSArray *arr = dic[cellContent];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArray[indexPath.section];
    NSArray *array = dic[cellContent];
    int type = [dic[cellType] intValue];
    
    if (type == CellTypeWithInfo) {
        NSDictionary *subDic = array[indexPath.row];
        TTDiamondDetailInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:infoCellID];
        cell.delegate = self;
        cell.dic = subDic;
        return cell;
        
    } else if (type == CellTypeWithZuanshiImageInfo) {
        TTDiamondDetailZuanshiImageInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:zuanshiCellID];
        cell.imageUrlsArray = array[indexPath.row];
        return cell;
        
    } else if (type == CellTypeWithMultyInfo) {
        NSDictionary *subDic = array[indexPath.row];
        if ([subDic[cellType] intValue] == CellTypeWithNormalInfo) {
            TTDiamondDetailInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:infoCellID];
            cell.delegate = self;
            cell.dic = subDic;
            return cell;
        } else if ([subDic[cellType] intValue] == CellTypeWithGrayInfo){
            TTDiamondDetailGrayInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:grayCellID];
            cell.dic = subDic;
            return cell;
        } else {
            TTDiamondDetailImageInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imageInfoCellID];
            cell.dic = subDic;
            cell.delegate = self;
            return cell;
        }
    } else if (type == CellTypeWithImage) {
        TTDetailImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imageCellID];
        cell.pictureView.image = array[indexPath.row];
        return cell;
    } else {
        // 列表（现在不要了）
        TTHomeListModel *model = array[indexPath.row];
        TTHomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCellID];
        cell.model = model;
        return cell;
    }
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSDictionary *dic = self.dataArray[indexPath.section];
//    NSArray *array = dic[cellContent];
//    int type = [dic[cellType] intValue];
//
//    if (type != CellTypeWithShopList) {
//        return;
//    }
//    TTHomeListModel *model = array[indexPath.row];
//    @weakify(self)
//    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"加入购物车提示" message:@"是否要将该钻直接加入购物车？" cancelTitle:@"不要了" cancelBlock:^{
//        @strongify(self)
//        [self lookShopDetailWithModel:model];
//    } confirmTitle:@"是的" confirmBlock:^BOOL{
//        @strongify(self)
//        // 加入购物车
//        @weakify(self)
//        [self addShopToCartWithButton:self.addCartBtn success:^{
//            @strongify(self)
//            [self lookShopDetailWithModel:model];
//        }];
//
//        return YES;
//    }];
//    [alertView showInWindowWithBackgoundTapDismissEnable:YES];
//
//
//}

- (void)lookShopDetailWithModel:(TTHomeListModel *)model
{
    TTGoodsDetailViewController *detailVC = [[TTGoodsDetailViewController alloc] initWithStyleLibraryId:model.StyleLibraryId model:nil];
//    detailVC.styleLibraryId = model.StyleLibraryId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArray[indexPath.section];
    int type = [dic[cellType] intValue];
    
    if (type == CellTypeWithInfo || type == CellTypeWithMultyInfo) {
        return 30;
    } else if (type == CellTypeWithZuanshiImageInfo) {
        CGFloat h = (SCREEN_WIDTH - 35) / 4.0;
        return h + 40;
    } else if (type == CellTypeWithImage) {
        return self.imageH;
    } else if (type == CellTypeWithShopList) {
        TTHomeListModel *model = dic[cellContent][indexPath.row];
        NSArray *contentArray = model.DescriptionList;
        CGFloat h = ceil(contentArray.count / 2.0) * 22 + 94;
        h = h < 145 ? 145 : h;
        return h * kScale + 20;
    }
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    int type = [dic[cellType] intValue];
    if (type == CellTypeWithInfo || type == CellTypeWithMultyInfo || type == CellTypeWithZuanshiImageInfo) {
        return 40;
    } else if (type == CellTypeWithShopList) {
        return 105;
    }
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    int type = [dic[cellType] intValue];
    
    if (type == CellTypeWithInfo) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
        view.backgroundColor = [UIColor whiteColor];
        TTDetailSectionLabel *label = [[TTDetailSectionLabel alloc] initWithFrame:CGRectMake(10, 0, 100, view.height) title:self.sectionTitles[section]];
        [view addSubview:label];
        [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.height - 5) endPoint:CGPointMake(self.view.width - 10, view.height - 5) lineColor:kSeparatorColor];
        if (eyeBtn_ == nil) {
            eyeBtn_ = [UIButton createButtonWithNormalImage:[UIImage imageNamed:@"eyeopen"] selectedImage:[UIImage imageNamed:@"eyeclose"] target:self action:@selector(eyeBtnClick:)];
            eyeBtn_.frame = CGRectMake(self.view.width - 50, 0, 40, view.height);
        }
        [view addSubview:eyeBtn_];
        return view;
    } else if (type == CellTypeWithMultyInfo) {
           UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
           view.backgroundColor = [UIColor whiteColor];
           TTDetailSectionLabel *label = [[TTDetailSectionLabel alloc] initWithFrame:CGRectMake(10, 0, 100, view.height) title:self.sectionTitles[section]];
           [view addSubview:label];
           if (self.model.Video.length > 0 && !self.hasPic) {
               UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 110, 0, 100, view.height) image:[UIImage imageNamed:@"btn_right_arrow"] title:@"播放视频" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:12] space:3 backgroundColor:[UIColor whiteColor] target:self action:@selector(playVideoAction)];
               [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
               [view addSubview:btn];
           }
           
           [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.height - 5) endPoint:CGPointMake(self.view.width - 10, view.height - 5) lineColor:kSeparatorColor];
           return view;
       } else if (type == CellTypeWithZuanshiImageInfo) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
        view.backgroundColor = [UIColor whiteColor];
        TTDetailSectionLabel *label = [[TTDetailSectionLabel alloc] initWithFrame:CGRectMake(10, 0, 100, view.height) title:self.sectionTitles[section]];
        [view addSubview:label];
        if (self.model.Video.length > 0 && !self.model.SouthAfrica) {
            UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 110, 0, 100, view.height) image:[UIImage imageNamed:@"btn_right_arrow"] title:@"播放视频" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:12] space:3 backgroundColor:[UIColor whiteColor] target:self action:@selector(playVideoAction)];
            [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
            [view addSubview:btn];
        }
        
        [LLLineUtilies addLineInView:view startPoint:CGPointMake(10, view.height - 5) endPoint:CGPointMake(self.view.width - 10, view.height - 5) lineColor:kSeparatorColor];
        return view;
    } else if (type == CellTypeWithImage) {
        return [UIView new];
    }
//    else if (type == CellTypeWithShopList) {
//        // 列表
//        if (self.listSectionView == nil) {
//            self.listSectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 105)];
//            self.listSectionView.backgroundColor = [UIColor whiteColor];
//            [LLLineUtilies addLineInView:self.listSectionView startPoint:CGPointMake(0, 5) endPoint:CGPointMake(self.view.width, 5) lineColor:kSeparatorColor lineWidth:10];
//            TTDetailSectionLabel *label = [[TTDetailSectionLabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40) title:@"该托可以镶嵌的石头"];
//            [self.listSectionView addSubview:label];
//            NSString *str = [NSString stringWithFormat:@"共%d粒", self.tuoCount];
//            self.countLabel = [UILabel labelWithTitle:str color:kMainColor fontSize:11];
//            self.countLabel.frame = CGRectMake(label.right, label.top, 120, label.height);
//            [self.listSectionView addSubview:self.countLabel];
//
//            [LLLineUtilies addLineInView:self.listSectionView startPoint:CGPointMake(10, label.bottom - 3) endPoint:CGPointMake(self.view.width - 10, label.bottom - 3) lineColor:kSeparatorColor];
//
//            TTDiamondDetailSectionView *secView = [[TTDiamondDetailSectionView alloc] initWithFrame:CGRectMake(0, label.bottom, self.view.width, 55) selectTitleBlock:^(TTDiamondDetailSectionView * _Nullable view, NSString *title) {
//                self.finished = NO;
//                for (UIView *view in self.notDataView.subviews) {
//                    view.hidden = YES;
//                }
//                NSMutableDictionary *dic = self.dataArray.lastObject;
//                NSMutableArray *array = dic[cellContent];
//                [array removeAllObjects];
//                [self.tableView reloadSection:self.dataArray.count - 1 withRowAnimation:UITableViewRowAnimationNone];
//
//                self.page = 1;
//                [self.paramsDic setObject:@(1) forKey:@"param.sourceType"];
//                [self.paramsDic setObject:title forKey:@"param.categoryName"];
//                [self.paramsDic setObject:@"false" forKey:@"param.isAsc"];
//                [self.paramsDic setObject:@(40) forKey:@"param.pageSize"];
//                [self queryOtherDataWithParams:self.paramsDic];
//            }];
//
//            [self.listSectionView addSubview:secView];
//
//        }
//
//        return self.listSectionView;
//    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    int type = [dic[cellType] intValue];
    
    if (type == CellTypeWithInfo || type == CellTypeWithZuanshiImageInfo) {
        return 10;
    } else if (type == CellTypeWithMultyInfo) {
//        return self.isOpen ? 0.1 : 30;
        return 30;;
    } else if (type == CellTypeWithImage) {
        return 0.1;
    }
//    return self.finished ? 0.1 : notDataViewH;
    return 0.1;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    int type = [dic[cellType] intValue];
    if (type == CellTypeWithInfo || type == CellTypeWithZuanshiImageInfo) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
        view.backgroundColor = kSeparatorColor;
        return view;
    } else if (type == CellTypeWithMultyInfo) {
        return [UIView new];
//        if (self.isOpen) {
//            return [UIView new];
//        } else {
//            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
//            view.backgroundColor = [UIColor whiteColor];
//            UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(80, 0, view.width - 160, view.height) image:[UIImage imageNamed:@"down_arrow"] title:@"购买须知" titleColor:UIColorHex(666666) titleFont:[UIFont systemFontOfSize:14] space:0 backgroundColor:nil target:self action:@selector(clickOtherParamsBtn)];
//            [view addSubview:btn];
//            return view;
//        }
    } else if (type == CellTypeWithImage) {
        return [UIView new];
    } else {
//        return self.finished ? [UIView new] : self.notDataView;
        return [UIView new];
    }
    
}

#pragma mark - TTDiamondDetailInfoViewCellDelegate
- (void)lookVervifyWithDetailInfoViewCell:(TTDiamondDetailInfoViewCell *)cell
{
    [TTLoginSingle lookCertificateWithUrlString:self.model.Certificate];
}

// 官网查询
- (void)queryWebsiteWithDetailInfoViewCell:(TTDiamondDetailInfoViewCell *)cell
{
    NSString *str;
    if ([self.model.Cert isEqualToString:@"GIA"]) {
        str = [NSString stringWithFormat:@"https://www.gia.edu/CN/report-check?reportno=%@&s=1562729836824", self.model.CertNo];
    } else if ([self.model.Cert isEqualToString:@"IGI"]) {
        str = [NSString stringWithFormat:@"https://igi.org/ch/verify.php?r=%@", self.model.CertNo];
    } else if ([self.model.Cert isEqualToString:@"HRD"]) {
        str = @"https://my.hrdantwerp.com/?";
    }
    if (str.length > 0) {
        NSURL *url = [NSURL URLWithString:str];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
    
}

#pragma mark - TTDiamondDetailImageInfoViewCellDelegate
- (void)lookImageWithUrlString:(NSString *)imageUrlString
{
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:imageUrlString];
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSourceArray = @[data];
    [browser show];
}

- (void)lookVideoWithUrlString:(NSString *)videoUrlString
{
    LLWebViewController *webVC = [[LLWebViewController alloc] init];
    webVC.webTitle = @"视频";
    [webVC setUrlString:videoUrlString htmlString:nil];
    [self.navigationController pushViewController:webVC animated:YES];
}

- (void)playVideoAction
{
    NSString *urlStr = [self.model.Video stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    LLWebViewController *webVC = [[LLWebViewController alloc] init];
    webVC.webTitle = @"视频";
    [webVC setUrlString:urlStr htmlString:nil];
    [self.navigationController pushViewController:webVC animated:YES];
}

//- (void)clickOtherParamsBtn
//{
//    self.isOpen = YES;
//
//    [self addPurchaseInformation];
//
//    [self.tableView reloadData];
//}

//- (void)addPurchaseInformation
//{
//    int i = 0;
//    for (NSDictionary *dic in self.dataArray) {
//        i++;
//        if ([dic[cellType] integerValue] == CellTypeWithMultyInfo) {
//            break;
//        }
//    }
//    [self.dataArray insertObject:@{cellType : @(CellTypeWithImage), cellContent : @[[UIImage imageNamed:@"luoshidetailzs"]]} atIndex:i];
//}

- (void)getDiamondDetailData
{
    if (self.model != nil) {
        [self setupDatas];
        return;
    }
    
    
    NSDictionary *dic = @{
                          @"param.strUniqueId" : self.diamondID
                          };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getDiamondDetailData_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        [TTLoginSingle sharedSingle].dollarRate = [responseObject[@"dollarRate"] floatValue];
        NSArray *arr = responseObject[@"data"];
        self.model = [TTDiamondModel modelWithJSON:arr.firstObject];
        
        if (self.model == nil) {
            [self.tableView reloadData];
        } else {
            [self setupDatas];
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        @strongify(self)
        
        [self.tableView reloadData];
        
    } showLoadingInView:self.view];
    
}

- (void)setupDatas
{
    UIImage *shareImage = [[UIImage imageNamed:@"fenxiang"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:shareImage style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    
    self.isInCart = NO;
    for (TTShopCartCodeModel *model in [TTLoginSingle sharedSingle].cartCodeArray) {
        NSString *code = [NSString stringWithFormat:@"%@_%@", self.model.CertNo, self.model.SupplierNo];
        if ([model.StockBarCode isEqualToString:code]) {
            self.isInCart = YES;
            break;
        }
    }
    
    self.paramsDic = [NSMutableDictionary dictionary];
    
    [self setupTableiewDatas];
    
    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        self.page++;
//        [self queryOtherDataWithParams:self.paramsDic];
//    }];
    
    [self.tableView reloadData];
    
    [self setupBottomView];
}

- (void)setupTableiewDatas
{
//    if (self.dataArray.count >= 1) {
//        [self.dataArray removeObjectsInRange:NSMakeRange(0, self.dataArray.count - 1)];
//    }
    [self.dataArray removeAllObjects];
    certNum_ = eyeBtn_.selected ? @"********" : self.model.CertNo;
    rmbPrice_ = eyeBtn_.selected ? @"****" : [NSString stringWithFormat:@"%@元", self.model.Price];
    dollarPrice_ = eyeBtn_.selected ? @"****" : [NSString stringWithFormat:@"%@$", self.model.Rate];
    internationalPrice_ = eyeBtn_.selected ? @"****" : [NSString stringWithFormat:@"%@$", self.model.Rate];
    discountStr_ = eyeBtn_.selected ? @"****" : self.model.Disc;
    yaomaStr_ = eyeBtn_.selected ? @"****" : self.model.Inscription;
    
    // 1. 检测报告
    NSMutableArray *reportArray = [NSMutableArray array];
    NSString *zsString = self.model.Certificate.length > 0 ? @"证书预览" : @"";
    if (self.model.SouthAfrica) {
        [reportArray addObject:[self createDetailInfoDicWithLeftTitle:@"证书类型" leftContent:self.model.Cert rightTitle:@"颁发日期" rightContent:self.model.AwardTime]];
        [reportArray addObject:[self createDetailInfoDicWithLeftTitle:@"钻石产地" leftContent:@"South Africa" rightTitle:@"" rightContent:@""]];
    } else {
        [reportArray addObject:[self createDetailInfoDicWithLeftTitle:@"证书类型" leftContent:self.model.Cert leftButtonTitle:zsString rightTitle:@"证书编号" rightContent:certNum_]];
        [reportArray addObject:[self createDetailInfoDicWithLeftTitle:@"颁发日期" leftContent:self.model.AwardTime rightTitle:@"" rightContent:@""]];
    }
    [self.dataArray addObject:@{cellType : @(CellTypeWithInfo), cellContent : reportArray}];
    
    self.sectionTitles = @[@"检查报告", @"钻石图片", @"钻石档案", @"", @""];
    // 1.1 裸石图片
    NSMutableArray *zuanshiArray = [NSMutableArray arrayWithObjects:self.model.ProductImage, self.model.OriginalImage, self.model.FeaturesImage, self.model.ProportionImage, nil];
    self.hasPic = NO;
    for (NSString *str in zuanshiArray) {
        if (str.length > 0) {
            self.hasPic = YES;
            break;
        }
    }
    if (self.hasPic) {
        [self.dataArray addObject:@{cellType : @(CellTypeWithZuanshiImageInfo), cellContent : @[zuanshiArray]}];
    } else {
        self.sectionTitles = @[@"检查报告", @"钻石档案", @"", @""];
    }
    
    // 2.
    NSMutableArray *infoArray = [NSMutableArray array];
    // TODO:基本信息
    NSString *timeStr = [NSString stringWithFormat:@"%@刷新", self.model.CreateTime];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"基本信息" rightTitle:timeStr]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"形状" leftContent:self.model.Shape rightTitle:@"尺寸" rightContent:self.model.Measurement]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"颜色" leftContent:self.model.Color rightTitle:@"重量" rightContent:[NSString stringWithFormat:@"%@ct", self.model.DiaSize]]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"净度" leftContent:self.model.Clarity rightTitle:@"奶色" rightContent:self.model.Milky]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"切工" leftContent:self.model.Cut rightTitle:@"咖色" rightContent:self.model.Browness]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"抛光" leftContent:self.model.Polish rightTitle:@"绿色" rightContent:self.model.Green]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"对称" leftContent:self.model.Sym rightTitle:@"黑色" rightContent:self.model.Black]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"荧光" leftContent:self.model.Flour rightTitle:@"肉眼净" rightContent:self.model.EyeClean]];
    // TODO:图片，视频
    //    if (self.model.Video.length > 1 || self.model.HavePicture) {
    //        [infoArray addObject:[self createDetailImageInfoDicWithLeftTitle:@"图片" leftImageName:@"miniimage" rightTitle:@"视频" rightImageName:@"minivideo" imageUrlString:self.model.DayLight videoUrlString:self.model.Video]];
    //    } else {
    //        [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"图片" leftContent:@"-" rightTitle:@"视频" rightContent:@"-"]];
    //    }
    
    // TODO: 价格
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"价格" rightTitle:nil]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"人民币/粒" leftContent:rmbPrice_ rightTitle:@"美金/粒" rightContent:dollarPrice_]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"国际报价" leftContent:internationalPrice_ rightTitle:@"折扣" rightContent:discountStr_]];
    
    NSString *status = shopStatusDic()[[NSString stringWithFormat:@"%d", self.model.SysStatus]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"状态" leftContent:status rightTitle:@"地点" rightContent:self.model.Location]];
    
    //TODO:比例
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"比例" rightTitle:nil]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"全深比" leftContent:self.model.FullDepth rightTitle:@"台宽比" rightContent:self.model.DiaTable]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"冠角" leftContent:self.model.CrownAngle rightTitle:@"冠高比" rightContent:self.model.CrownHeight]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"亭角" leftContent:self.model.PavilionAngle rightTitle:@"亭深比" rightContent:self.model.DiaDepth]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"星小面比" leftContent:self.model.StarFacet rightTitle:@"下腰小面比" rightContent:self.model.LowerLumbarFacet]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"腰棱" leftContent:self.model.Girdle rightTitle:@"" rightContent:@""]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"底尖" leftContent:self.model.Culet rightTitle:@"" rightContent:@""]];
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"评语" leftContent:self.model.Comment rightTitle:@"" rightContent:@""]];
    
    //TODO:净度特征
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"净度特征" rightTitle:nil]];
    NSString *value = @"";
    if (self.model.ClarityCharacteristics.length > 1) {
        NSDictionary *dic = [TTCommondDataModel getClarityCharacteristics];
        NSArray *characteristicsArray = [self.model.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            if (dic[key]) {
                value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
            }
        }
    }
    value = value ? : self.model.ClarityCharacteristics;
    [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"净度特征" leftContent:value rightTitle:@"" rightContent:@""]];
    
    
    //TODO:腰码
    if (!self.model.SouthAfrica) {
        [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"腰码" rightTitle:nil]];
        [infoArray addObject:[self createDetailInfoDicWithLeftTitle:@"腰码" leftContent:yaomaStr_ rightTitle:@"" rightContent:@""]];
    }
    
    [self.dataArray addObject:@{cellType : @(CellTypeWithMultyInfo), cellContent : infoArray}.mutableCopy];
    
    
    [self.dataArray addObject:@{cellType : @(CellTypeWithImage), cellContent : @[[UIImage imageNamed:@"luoshidetailzs"]]}];

    
    // 3. 列表
//    BOOL hasList = NO;
//    for (NSDictionary *dic in self.dataArray) {
//        if ([dic[cellType] integerValue] == CellTypeWithShopList) {
//            hasList = YES;
//            break;
//        }
//    }
//    if (!hasList) {
//        [self.dataArray addObject:@{cellType : @(CellTypeWithShopList)}.mutableCopy];
//    } else {
//        // 把列表放到最后面
//        NSDictionary *dic = self.dataArray.firstObject;
//        [self.dataArray removeFirstObject];
//        [self.dataArray addObject:dic];
//    }
}

//- (void)queryOtherDataWithParams:(NSMutableDictionary *)dic
//{
//    [dic setObject:@(self.page) forKey:@"param.pageIndex"];
//
//    @weakify(self)
//    [[LLNetworkManagerTool sharedManager] postUrlString:queryList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        @strongify(self)
//
//        self.finished = YES;
//        NSMutableDictionary *dic = self.dataArray.lastObject;
//        NSMutableArray *array = dic[cellContent];
//        if (array == nil) {
//            array = [NSMutableArray array];
//        }
//
//        self.tuoCount = [responseObject[@"TotalCount"] intValue];
//        NSString *str = [NSString stringWithFormat:@"共%d粒", self.tuoCount];
//        self.countLabel.text = str;
//
//        NSArray *arr = responseObject[@"Data"];
//        if (self.page == 1) {
//            [array removeAllObjects];
//        }
//
//        for (NSDictionary *dic in arr) {
//            TTHomeListModel *model = [TTHomeListModel modelWithJSON:dic];
//            [array addObject:model];
//        }
//
//        dic[cellContent] = array;
//
//        if (arr.count < 40) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        } else {
//            [self.tableView.mj_footer endRefreshing];
//        }
//
//
////        [self.tableView reloadSection:self.dataArray.count - 1 withRowAnimation:UITableViewRowAnimationFade];
//        [self.tableView reloadData];
//
//    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
//
//        self.finished = NO;
//
//        for (UIView *view in self.notDataView.subviews) {
//            view.hidden = NO;
//        }
//
//        [self.tableView.mj_footer endRefreshing];
//
//        [self.tableView reloadSection:self.dataArray.count - 1 withRowAnimation:UITableViewRowAnimationFade];
//
//    } showLoadingInView:self.notDataView];
//}

// 加入购物车
- (void)addCartBtnClick:(UIButton *)sender
{
    if (sender.selected) {
        // 从购物车删除
        [self deleteShopFromCartWithButton:sender];
        
    } else {
        // 加入购物车
        [self addShopToCartWithButton:sender success:nil];
    }
    
}

// 添加进购物车
- (void)addShopToCartWithButton:(UIButton *)button success:(nullable dispatch_block_t)success
{
    NSString *title = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@", self.model.Shape, self.model.DiaSize, self.model.Color, self.model.Clarity, self.model.Cut, self.model.Sym, self.model.Polish, self.model.Flour];
    CGFloat diaSize = self.model.DiaSize.floatValue * 100;
    CGFloat insertSize = [TTLoginSingle findDiamondSizeWithSize:diaSize];
    NSDictionary *mDic = @{
                           @"GoodsType": @(1),
                           @"GoodsTitle": title,
                           @"GoodsImage": self.model.Shape,
                           @"GoodsBarCode": self.model.GoodId,
                           @"DiaRef": self.model.DiaRef,
                           @"SupplierNo": self.model.SupplierNo,
                           @"CertNo": self.model.CertNo,
                           @"AddPrice": self.model.Price,
                           @"InsertSize": @(insertSize),
                           @"Number": @(1),
                           @"BuyNumberMax": @(1)
                           };
    NSDictionary *dic = @{
                          @"GoodsList" : @[mDic]
                          };
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:addShopCart_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [MBProgressHUD showSuccess:@"已加入购物车"];
        
        button.selected = YES;
        button.backgroundColor = UIColorHex(666666);
        
        self.model.isInCart = YES;
        
        [TTCommonNetData handleShopCartCodeInfoWithArray:responseObject];
        
        self.shopCartView.count = [TTLoginSingle sharedSingle].shopCartCount;
        
        self.statusChange = YES;
        
        // 购物车刷新
        [[TTLoginSingle sharedSingle].shopCartSubject sendNext:@(1)];
        if (success) {
            success();
        }
        
        NSLog(@"添加成功");
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        NSLog(@"添加失败");
        button.selected = NO;
        self.model.isInCart = NO;
        [MBProgressHUD showError:@"加入购物车失败"];
        if (success) {
            success();
        }
        
    } showLoadingInView:self.view];
}

// 从购物车删除
- (void)deleteShopFromCartWithButton:(UIButton *)button
{
    NSDictionary *dic = @{
                          @"DiaRef" : self.model.DiaRef,
                          @"CertNo" : self.model.CertNo,
                          @"SupplierNo" : self.model.SupplierNo
                          };
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:deleteShopcart_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        button.selected = NO;
        button.backgroundColor = UIColorHex(6928BA);
        
        self.model.isInCart = NO;
        
        // 购物车刷新
        [[TTLoginSingle sharedSingle].shopCartSubject sendNext:@(1)];
        [TTCommonNetData handleShopCartCodeInfoWithArray:responseObject[@"StockBarCodeList"]];
        
        self.shopCartView.count = [TTLoginSingle sharedSingle].shopCartCount;
        self.statusChange = YES;
        NSLog(@"--> 删除成功");
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        NSLog(@"--> 删除失败");
        button.selected = YES;
        self.model.isInCart = YES;
        
    } showLoadingInView:self.view];
}

// 立即购买
- (void)buyBtnClick
{
    TTShopCartSubDetailModel *model = [TTCommondDataModel diamondModelToShopCartSubDetailModelWithModel:self.model];
    TTCommitOrderViewController *orderVC = [[TTCommitOrderViewController alloc] initWithShopArray:@[model] diamondModel:nil];
    [self.navigationController pushViewController:orderVC animated:YES];
}

// 底部view
- (void)setupBottomView
{
    if (self.bottomView) return;
    
    _bottomView = [[UIView alloc] init];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(bottomViewH);
    }];
    
    [self.view layoutIfNeeded];
    [LLLineUtilies addLineInView:_bottomView startPoint:CGPointZero endPoint:CGPointMake(_bottomView.width, 0) lineColor:kSeparatorColor];
    
    CGFloat btnW = _bottomView.width * 0.4 * 0.5;
    @weakify(self)
    TTShopCartView *kefuView = [[TTShopCartView alloc] initWithFrame:CGRectMake(0, 2, btnW, 48) imageName:@"kefu" title:@"联系客服" clickShopCartBlock:^(TTShopCartView *cartView) {
        @strongify(self)
        if (![TTLoginSingle sharedSingle].isLogin) {
            // 登录
            [self presentLoginViewController];
            return;
        }
        // 打电话
        [[TTLoginSingle sharedSingle].loginModel.SalesmanMobile callPhone];
        
    }];
    [_bottomView addSubview:kefuView];
    
    
    _shopCartView = [[TTShopCartView alloc] initWithFrame:CGRectMake(kefuView.right, kefuView.top, btnW, kefuView.height) imageName:@"shopcart" title:@"购物车" clickShopCartBlock:^(TTShopCartView *cartView) {
        @strongify(self)
        
        if (![TTLoginSingle sharedSingle].isLogin) {
            [self presentLoginViewController];
            return;
        }
        
        BOOL has = NO;
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[TTShopCartViewController class]]) {
                has = YES;
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
        if (!has) {
            TTShopCartViewController *shopCartVC = [[TTShopCartViewController alloc] init];
            [self.navigationController pushViewController:shopCartVC animated:YES];
        }
        
    }];
    _shopCartView.count = [TTLoginSingle sharedSingle].shopCartCount;
    [_bottomView addSubview:_shopCartView];
    
    UIButton *addCartBtn = [UIButton createButtonWithTitle:@"加入购物车" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:15] backgroundColor:UIColorHex(6928BA) target:self action:@selector(addCartBtnClick:)];
    [addCartBtn setTitle:@"取消加入" forState:UIControlStateSelected];
    [addCartBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    addCartBtn.isNeedLogin = YES;
    addCartBtn.selected = self.isInCart;
    if (self.isInCart) {
        addCartBtn.backgroundColor = UIColorHex(666666);
    }
    addCartBtn.frame = CGRectMake(_shopCartView.right, 0, _bottomView.width * 0.6 * 0.5, _bottomView.height);
    [_bottomView addSubview:addCartBtn];
    self.addCartBtn = addCartBtn;
    
    UIButton *buyBtn = [UIButton createButtonWithTitle:@"立即购买" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:15] backgroundColor:UIColorHex(FF1F1F) target:self action:@selector(buyBtnClick)];
    buyBtn.frame = CGRectMake(addCartBtn.right, 0, addCartBtn.width, _bottomView.height);
    buyBtn.isNeedLogin = YES;
    [_bottomView addSubview:buyBtn];
    
}

// 正常的
- (NSDictionary *)createDetailInfoDicWithLeftTitle:(NSString *)leftTitle leftContent:(NSString *)leftContent rightTitle:(NSString *)rightTitle rightContent:(NSString *)rightContent
{
    
    return @{
             @"leftTitle" : leftTitle,
             @"leftContent" : leftContent,
             @"rightTitle" : rightTitle,
             @"rightContent" : rightContent,
             cellType : @(CellTypeWithNormalInfo)
             };
}

// 包含按钮的
- (NSDictionary *)createDetailInfoDicWithLeftTitle:(NSString *)leftTitle leftContent:(NSString *)leftContent leftButtonTitle:(NSString *)leftButtonTitle rightTitle:(NSString *)rightTitle rightContent:(NSString *)rightContent
{
    
    return @{
             @"leftTitle" : leftTitle,
             @"leftContent" : leftContent,
             @"leftButtonTitle" : leftButtonTitle,
             @"rightTitle" : rightTitle,
             @"rightContent" : rightContent,
             cellType : @(CellTypeWithNormalInfo)
             };
}

// 灰色背景的
- (NSDictionary *)createDetailInfoDicWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle
{
    
    return @{
             @"leftTitle" : leftTitle,
             @"rightTitle" : rightTitle,
             cellType : @(CellTypeWithGrayInfo)
             };
}

// 文字图片的
- (NSDictionary *)createDetailImageInfoDicWithLeftTitle:(NSString *)leftTitle leftImageName:(NSString *)leftImageName rightTitle:(NSString *)rightTitle rightImageName:(NSString *)rightImageName imageUrlString:(NSString *)imageUrlString videoUrlString:(NSString *)videoUrlString
{
    
    return @{
             @"leftTitle" : leftTitle,
             @"leftContent" : leftImageName,
             @"rightTitle" : rightTitle,
             @"rightContent" : rightImageName,
             @"imageUrlString" : imageUrlString,
             @"videoUrlString" : videoUrlString,
             cellType : @(CellTypeWithImageInfo)
             };
}

- (void)eyeBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;

    [self setupTableiewDatas];

//    if (self.isOpen) {
//        [self addPurchaseInformation];
//    }

    [self.tableView reloadData];
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.emptyImageName = @"nodata";
        _tableView.emptyTitel = @"暂无数据";
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        [_tableView registerClass:[TTDiamondDetailInfoViewCell class] forCellReuseIdentifier:infoCellID];
        [_tableView registerClass:[TTDiamondDetailGrayInfoViewCell class] forCellReuseIdentifier:grayCellID];
        [_tableView registerClass:[TTDiamondDetailImageInfoViewCell class] forCellReuseIdentifier:imageInfoCellID];
        [_tableView registerClass:[TTDetailImageViewCell class] forCellReuseIdentifier:imageCellID];
        [_tableView registerClass:[TTHomeListTableViewCell class] forCellReuseIdentifier:listCellID];
        [_tableView registerClass:[TTDiamondDetailZuanshiImageInfoViewCell class] forCellReuseIdentifier:zuanshiCellID];
        [self.view addSubview:_tableView];
        _tableView.tableFooterView = [UIView new];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(-bottomViewH);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//- (UIView *)notDataView
//{
//    if (!_notDataView) {
//        _notDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, notDataViewH)];
//        CGFloat w = 150;
//        CGFloat x = (self.view.width - w) / 2;
//        UIImageView *noDataImageV = [[UIImageView alloc] initWithFrame:CGRectMake(x, 50, w, 90)];
//        noDataImageV.contentMode = UIViewContentModeScaleAspectFit;
//        noDataImageV.image = [UIImage imageNamed:@"nodata"];
//        noDataImageV.hidden = YES;
//        [_notDataView addSubview:noDataImageV];
//        
//        UILabel *label = [UILabel labelTextCenterWithTitle:@"没有相关内容~" color:UIColorHex(666666) fontSize:14];
//        label.hidden = YES;
//        [_notDataView addSubview:label];
//        
//    }
//    return _notDataView;
//}


NSDictionary *shopStatusDic() {
    return @{
             @"0" : @"订货",
             @"1" : @"在库",
             @"2" : @"预售",
             @"3" : @"借出",
             @"4" : @"锁定",
             @"9" : @"已售",
             };
}


@end
